<?php
class CdeAnswersController extends AppController {
	var $name = 'CdeAnswers';
	var $helpers = array('Html', 'Form');

	// Add new answer for module - DONE
	function add($moduleId = null) {
		// Check if the module ID is provided, if not display error message and redirect to index of all units
		if (!$moduleId && empty($this->data)) {
			$this->Session->setFlash(__('Module ID is required', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		}
		
		$this->set('cdeModule', $this->CdeAnswer->CdeModule->read(null, $moduleId));

		if (!empty($this->data)) {
			$this->CdeAnswer->create();
			if ($this->CdeAnswer->save($this->data)) {
				// Add new answer successfully, redirect back to manage page of the module
				$this->Session->setFlash(__('The new answer has been successfully added', true), 'default', array('class' => 'success-message'));
				$this->redirect(array('action'=>'../cde_Modules/manage/'.$this->data['CdeAnswer']['cde_module_id']));
			} else {
				// Error occurred, cannot add new answer
				$this->Session->setFlash(__('An error occurred, new answer could not be successfully added. Please try again!', true), 'default', array('class' => 'error-message'));
			}
		}
	}

	// Update answer - DONE
	function edit($id = null) {
		// Check if the answer ID is provided, if not display error message and redirect to index of all units
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Answer ID is required', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		}

		// Get the answer by ID
		$cdeAnswer = $this->CdeAnswer->read(null, $id);
		if (!$cdeAnswer && empty($this->data)) {
			// If the answer is not found display the error message and redirect to index of all units
			$this->Session->setFlash(__('Invalid answer ID, cannot edit', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		} else {
			$this->set('cdeAnswer', $cdeAnswer);
			if (!empty($this->data)) {
				// On update action
				if ($this->CdeAnswer->save($this->data)) {
					// Update successfully, redirect back to manage page of the module
					$this->Session->setFlash(__('The answer has been successfully updated', true), 'default', array('class' => 'success-message'));
					$this->redirect(array('action'=>'../cde_modules/manage/'.$cdeAnswer['CdeAnswer']['cde_module_id']));
				} else {
					// Error occurred, cannot update
					$this->Session->setFlash(__('The answer could not be updated. Please try again!', true), 'default', array('class' => 'error-message'));
				}
			} else {
				// Set the data to update
				$this->data = $cdeAnswer;
			}
		}
	}

	// Delete answer - DONE
	function delete($id = null) {
		// Check if the answer ID is provided, if not display error message and redirect to index of all units
		if (!$id) {
			$this->Session->setFlash(__('Answer ID is required', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		}

		// Get the answer by ID
		$cdeAnswer = $this->CdeAnswer->read(null, $id);
		if ($cdeAnswer != null) {
			// If the answer is found, delete it
			if ($this->CdeAnswer->del($id)) {
				// Delete successfully, redirect back to manage page of the module
				$this->Session->setFlash(__('The answer has been successfully deleted', true), 'default', array('class' => 'success-message'));
				$this->redirect(array('action'=>'../cde_modules/manage/'.$cdeAnswer['CdeAnswer']['cde_module_id']));
			} else {
				// Error occurred, cannot delete, redirect back to manage page of the module
				$this->Session->setFlash(__('An error occurred, cannot delete the answer. Please try again!', true), 'default', array('class' => 'error-message'));
				$this->redirect(array('action'=>'../cde_modules/manage/'.$cdeAnswer['CdeAnswer']['cde_module_id']));
			}
		} else {
			// If the answer is not found display the error message and redirect to index of all units
			$this->Session->setFlash(__('Invalid answer ID, cannot delete', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		}
	}

	// Disable answer - DONE
	function disable($id = null) {
		// Check if the answer ID is provided, if not display error message and redirect to index of all units
		if (!$id) {
			$this->Session->setFlash(__('Answer ID is required', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		}

		// Get the answer by ID
		$cdeAnswer = $this->CdeAnswer->read(null, $id);
		if ($cdeAnswer != null) {
			// If the answer is found, disable it
			$this->CdeAnswer->id = $id;
			if ($this->CdeAnswer->saveField('enabled', '0', true)) {
				// Disable successfully, redirect back to manage page of the module
				$this->Session->setFlash(__('The answer has been successfully disabled', true), 'default', array('class' => 'success-message'));
				$this->redirect(array('action'=>'../cde_modules/manage/'.$cdeAnswer['CdeAnswer']['cde_module_id']));
			} else {
				// Error occurred, cannot disable, redirect back to manage page of the module
				$this->Session->setFlash(__('An error occurred, cannot disable the answer. Please try again!', true), 'default', array('class' => 'error-message'));
				$this->redirect(array('action'=>'../cde_modules/manage/'.$cdeAnswer['CdeAnswer']['cde_module_id']));
			}
		} else {
			// If the answer is not found display the error message and redirect to index of all units
			$this->Session->setFlash(__('Invalid answer ID, cannot disable', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		}
	}
	
	// Enable answer - DONE
	function enable($id = null) {
		// Check if the answer ID is provided, if not display error message and redirect to index
		if (!$id) {
			$this->Session->setFlash(__('Answer ID is required', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		}

		// Get the answer by ID
		$cdeAnswer = $this->CdeAnswer->read(null, $id);
		if ($cdeAnswer != null) {
			// If the unit is found, enable it
			$this->CdeAnswer->id = $id;
			if ($this->CdeAnswer->saveField('enabled', '1', true)) {
				// Enable successfully, redirect back to manage page of the module
				$this->Session->setFlash(__('The answer has been successfully enabled', true), 'default', array('class' => 'success-message'));
				$this->redirect(array('action'=>'../cde_modules/manage/'.$cdeAnswer['CdeAnswer']['cde_module_id']));
			} else {
				// Error occurred, cannot enable, redirect back to manage page of the module
				$this->Session->setFlash(__('An error occurred, cannot enable the answer. Please try again!', true), 'default', array('class' => 'error-message'));
				$this->redirect(array('action'=>'../cde_modules/manage/'.$cdeAnswer['CdeAnswer']['cde_module_id']));
			}
		} else {
			// If the unit is not found display the error message and redirect to index
			$this->Session->setFlash(__('Invalid answer ID, cannot enable', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		}
	}
}
?>